#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using AirWeb.DirectoryMgr;
using AirWeb.ApplicationTools;

namespace FlashBack.Web
{
	/// <summary>
	/// Summary description for MovieShow.
	/// </summary>
	public partial class MovieShow : System.Web.UI.Page
	{

		DirectoryLister m_Lister = new DirectoryLister();
		DirectoryHelper m_dirHelper;
		public string m_Selected;
		public string m_PlayerRendering;
		public string m_DownloadPlayer;
		public string m_download;
	
		public MovieShow()
		{
			m_dirHelper = new DirectoryHelper();
			Page.Init += new System.EventHandler(Page_Init);
		}

		protected void Page_Load(object sender, System.EventArgs e)
		{
			string request = Request.QueryString.Get("path");

			if (!IsPostBack)
			{
				string dir = m_dirHelper.CreateOriginalPath(request);
				m_Lister.Comparer = new FileComparer(CompareBy.CreationDate,SortOrder.Ascending);

				DataTable dt = new DataTable();
				DataRow dr;
				dt.Columns.Add(new DataColumn("ShortName", typeof(string)));
				dt.Columns.Add(new DataColumn("OrgPath", typeof(string)));
				dt.Columns.Add(new DataColumn("MovieSize", typeof(string)));
				dt.Columns.Add(new DataColumn("MovieDate", typeof(string)));

				ArrayList l = m_Lister.ListFiles(Server.MapPath(dir),0);

				string t = request + " - " + l.Count + " movies";
				title.Text = t.Replace("/"," > ");

				System.Collections.IEnumerator myEnumerator = l.GetEnumerator();

				while ( myEnumerator.MoveNext() )
				{
					FileInfo f = (FileInfo)myEnumerator.Current;
					dr = dt.NewRow();

					String tnpath = m_dirHelper.ReverseMapPath(f.FullName,m_dirHelper.OriginalsVirtualRoot);
					dr[0] = f.Name;
					dr[1] = tnpath;

					long filesize;

					if(f.Length >= 1024*1024)
					{
						filesize = f.Length / (1024*1024);
						dr[2] = filesize.ToString() + "&nbsp;MB";
					}
					else
					{
						filesize = f.Length / 1024;
						dr[2] = filesize.ToString() + "&nbsp;K";
					}

					dr[3] = f.CreationTime.ToShortDateString();

					if(m_Selected == null)
					{
						m_Selected = dr[1].ToString();
					}

					dt.Rows.Add(dr);
				}

				movieList.DataSource = new DataView(dt);
				movieList.DataBind();

				SelectMovieInternal();
			}

			m_download = "DetailList.aspx?path=" + request;
		}

		protected void Page_Init(object sender, EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
		}

		#region Web Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    

		}
		#endregion

		public void SelectMovie (object sender, CommandEventArgs e)
		{
			m_Selected = e.CommandName.ToString();
			SelectMovieInternal();
		}
		
		void SelectMovieInternal()
		{
			if(m_Selected == null)
			{
				m_PlayerRendering = "<h4>There are no movies in this album.</h4>";
				m_DownloadPlayer = "";
				DownloadLink.Visible = false;

				return;
			}
			
			if(m_Selected.IndexOf(".MOV") != -1)
			{
 				m_PlayerRendering = "<embed 'viewer' bgcolor='#FFFFCC' width='400' autoplay='false' enablejavascript='true' TARGET='MYSELF' src='";
				m_PlayerRendering += m_Selected;
				m_PlayerRendering += " ' height='300' pluginspage='http://www.apple.com/quicktime/download/index.html' CONTROLLER='TRUE' cache='true'>";

				m_DownloadPlayer = "You need a Quick Time plugin to view this movie.<br>If you can't see a picture, please click <A target='_new' href='http://www.apple.com/quicktime/download/index.html'>here</A> to download the plugin form Apple site.";
			}
			else
			{
				String path = Request.Url.Scheme + "://" + Request.Url.Authority + m_Selected;

				m_PlayerRendering = "<OBJECT ID='Player' width='350' height='340' CLASSID='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'>";
				m_PlayerRendering += "<PARAM name='URL' value='" + path + "'>";
				m_PlayerRendering += "<PARAM name='uiMode' value='full'></OBJECT>";

				m_DownloadPlayer = "You need Windows Media 7 to view this movie.<br>If you can't see a picture, please click <A target='_new' href='http://www.microsoft.com/windows/windowsmedia/en/download/default.asp'>here</A> to download the player form Microsoft site.";
			}

			System.Collections.IEnumerator myEnumerator = movieList.Controls.GetEnumerator();
			while ( myEnumerator.MoveNext() )
			{
				Object t  = myEnumerator.Current;
				DataListItem a = (DataListItem)myEnumerator.Current;
				LinkButton l = (LinkButton)a.Controls[1];
				if(l.CommandName == m_Selected)
					l.CssClass = "MovieStripHi";
				else
					l.CssClass = "MovieStrip";
			}
		}
	}
}
